package com.dbq.clock.rest.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dbq.clock.common.api.CommonPage;
import com.dbq.clock.common.api.CommonResult;
import com.dbq.clock.rest.user.model.UmsMember;
import com.dbq.clock.rest.user.service.UmsMemberService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author dabaoqiang
 * @since 2024-04-22
 */
@Slf4j
@RestController
@RequestMapping("/user/umsMember")
public class UmsMemberController {

    @Resource
    private UmsMemberService umsMemberService;

    @ApiOperation("添加用户")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult createBrand(@RequestBody UmsMember umsMember) {
        CommonResult commonResult;
        boolean result = umsMemberService.save(umsMember);
        if (result) {
            commonResult = CommonResult.success(umsMember);
            log.debug("createBrand success:{}", umsMember);
        } else {
            commonResult = CommonResult.failed("操作失败");
            log.debug("createBrand failed:{}", umsMember);
        }
        return commonResult;
    }

    @ApiOperation("更新指定id用户信息")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public CommonResult updateBrand(@RequestBody UmsMember umsMember) {
        CommonResult commonResult;
        boolean result = umsMemberService.updateById(umsMember);
        if (result) {
            commonResult = CommonResult.success(umsMember);
            log.debug("updateBrand success:{}", umsMember);
        } else {
            commonResult = CommonResult.failed("操作失败");
            log.debug("updateBrand failed:{}", umsMember);
        }
        return commonResult;
    }

    @ApiOperation("删除指定id的用户")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public CommonResult deleteBrand(@PathVariable("id") Long id) {
        boolean result = umsMemberService.removeById(id);
        if (result) {
            log.debug("deleteBrand success :id={}", id);
            return CommonResult.success(null);
        } else {
            log.debug("deleteBrand failed :id={}", id);
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("分页查询用户列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsMember>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1")
                                                         @ApiParam("页码") Integer pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "3")
                                                         @ApiParam("每页数量") Integer pageSize) {
        Page<UmsMember> page = new Page<>(pageNum, pageSize);
        Page<UmsMember> pageResult = umsMemberService.page(page);
        return CommonResult.success(CommonPage.restPage(pageResult));
    }

    @ApiOperation("获取指定id的用户详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsMember> brand(@PathVariable("id") Long id) {
        return CommonResult.success(umsMemberService.getById(id));
    }

}

